package com.woniuxy.ticketorder.service.impl;


import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniuxy.ticketorder.entity.Ticket_order;
import com.woniuxy.ticketorder.mapper.Ticket_orderMapper;
import com.woniuxy.ticketorder.service.Ticket_orderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.Random;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author order
 * @since 2021-02-22
 */
@Service
public class Ticket_orderServiceImpl extends ServiceImpl<Ticket_orderMapper, Ticket_order> implements Ticket_orderService {

    @Autowired
    Ticket_orderMapper ticket_orderMapper;

    @Override
    public String addOrder(String hall, String seat, String cinema, String price, Integer num, Integer userid) {

        Ticket_order order = new Ticket_order();

        order.setOrderno(generateNo());
        order.setCinema(cinema);
        order.setHall(hall);
        BigDecimal money = new BigDecimal(0);
        money = BigDecimal.valueOf(Integer.parseInt(price) * num);
        order.setMoney(money);
        order.setOrdertime(LocalDateTime.now());
        order.setUserid(userid);
        order.setStatus("0");

        return "ok";

    }


    //自动生成订单编号的工具类
    public static String generateNo() {

        String no = "WN";
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddhhmmss");

        no += dateFormat.format(new Date());

        int n = new Random().nextInt(10000);//0-9999
        //不够4位数自动补0
        for (int i = 0; i < 4 - (n + "").length(); i++) {
            no += "0";
        }

        return no + n;
    }
}
